home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / proff.zip / LOOK.C < prev    next >
C/C++ Source or Header  |  1988-02-12  |  3KB  |  160 lines

  1. /*
  2.  * from K&R "The C Programming language"
  3.  * Table lookup routines
  4.  *
  5.  */
  6. #include <stdio.h>
  7. #include "lookup.h"
  8. /*
  9.  * hash - for a hash value for string s
  10.  *
  11.  */
  12. hash(s)
  13. char *s;
  14. {
  15.     int    hashval;
  16.  
  17.     for (hashval = 0; *s != '\0';)
  18.         hashval += *s++;
  19.     return (hashval % HASHMAX);
  20. }
  21.  
  22. /*
  23.  * lookup - lookup for a string s in the hash table
  24.  *
  25.  */
  26. struct hashlist
  27. *lookup(s, hashtab)
  28. char *s;
  29. struct hashlist *hashtab[];
  30. {
  31.     struct hashlist *np;
  32.  
  33.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  34.         if (strcmp(s, np->name) == 0)
  35.             return(np);    /* found     */
  36.     return(NULL);        /* not found */
  37. }
  38.  
  39. /*
  40.  * install - install a string name in hashtable and its value def
  41.  * at a given hashtable.
  42.  */
  43. struct hashlist
  44. *install(name,def,hashtab)
  45. char *name;
  46. char *def;
  47. struct hashlist *hashtab[];
  48. {
  49.     int hashval;
  50.     struct hashlist *np, *lookup();
  51.     char *strsave(), *malloc();
  52.  
  53.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  54.         np = (struct hashlist *) malloc(sizeof(*np));
  55.                 if (np == NULL)
  56.             return(NULL);
  57.         if ((np->name = strsave(name)) == NULL)
  58.             return(NULL);
  59.         hashval = hash(np->name);
  60.         np->next = hashtab[hashval];
  61.         hashtab[hashval] = np;
  62.     } else                    /* found..     */
  63.         free(np->def);            /* free prev.  */
  64.     if ((np->def = strsave(def)) == NULL)
  65.         return(NULL);
  66.     return(np);
  67. }
  68.  
  69. /*
  70.  * strsave - save string s somewhere
  71.  *
  72.  */
  73. char
  74. *strsave(s)
  75. char *s;
  76. {
  77.     char *p, *malloc();
  78.     register int n;
  79.  
  80.     n = strlen(s) + 1;
  81.     if ((p = malloc(n)) != NULL)
  82.             strcpy(p, s);
  83.     return(p);
  84. }
  85.  
  86. /*
  87.  * lexinstal - instal a string name in hashtable and its value
  88.  *           used by lexical analyser to quickly match a token
  89.  *           and return its lexical value.
  90.  *
  91.  */
  92. struct lexlist
  93. *lexinstal(name,val,flag,lextable)
  94. char *name;
  95. int val;
  96. int flag;
  97. struct lexlist *lextable[];
  98. {
  99.     int hashval;
  100.     struct lexlist *np,*lexlook();
  101.     char *strsave(), *malloc();
  102.  
  103.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  104.         np = (struct lexlist *) malloc(sizeof(*np));
  105.         if (np == NULL)
  106.             return(NULL);
  107.         if ((np->name = strsave(name)) == NULL)
  108.             return(NULL);
  109.         hashval = hash(np->name);
  110.         np->link = lextable[hashval];
  111.         lextable[hashval] = np;
  112.     }
  113.     np->val = val;                /* replace prev */
  114.     np->flag = flag;
  115.     return(np);
  116. }
  117.  
  118. /*
  119.  * lexlook - lookup for a string s in the hash table
  120.  *         used by lexinstal only.
  121.  *
  122.  */
  123. struct lexlist
  124. *lexlook(s,table)
  125. char *s;
  126. struct lexlist *table[];
  127. {
  128.     struct lexlist *np;
  129.  
  130.     for (np = table[hash(s)]; np != NULL; np = np->link)
  131.         if (strcmp(s, np->name) == 0)
  132.             return(np);    /* found     */
  133.     return(NULL);        /* not found */
  134. }
  135.  
  136. /*
  137.  * remove an item from the hash table forever
  138.  *
  139.  */
  140. struct lexlist
  141. *remove(s, table)
  142. char *s;
  143. struct lexlist *table[];
  144. {
  145.     struct lexlist *np, *xp;
  146.  
  147.     np = table[hash(s)];
  148.     xp = np; 
  149.     while (np != NULL) {
  150.         if (strcmp(s, np->name) == 0) {
  151.             xp->link = np->link;    /* remove the link */
  152.             return(np);        /* return the lost */
  153.         }
  154.         xp = np; 
  155.         np = np->link;
  156.     }
  157.     return(NULL);
  158. }
  159.  
  160.